package org.person.gift.strategy.impl;



import org.person.gift.strategy.IShopCarDataSyncKeyGroupStrategy;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 按照hash策略对redis中的key做分组
 *
 */
public class ShopCarDataSyncKeyHashGroupStrategy implements IShopCarDataSyncKeyGroupStrategy {

    private Integer num = 100;

    public ShopCarDataSyncKeyHashGroupStrategy(){

    }

    public ShopCarDataSyncKeyHashGroupStrategy(Integer num){
        this.num = num;
    }

    @Override
    public Map<String, List<String>> group(Set<String> keys) {
        Map<String, List<String>> listMap = keys.stream().collect(
                Collectors.groupingBy(x -> (x.hashCode() % this.num) + ""));
        return listMap;
    }
}
